package com.easymin.weixin.controller.v4;

import com.easymin.weixin.common.api.DaijiaApi;
import com.easymin.weixin.entity.WxApplication;
import com.easymin.weixin.manager.FreightManager;
import com.easymin.weixin.manager.WxApplicationManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@SuppressWarnings("all")
@RestController("freightOrderResV4")
@RequestMapping("api/freight/v4")
public class FreightOrderRes {
  @Autowired
  private FreightManager freightManager;

  @Autowired
  private WxApplicationManager wxApplicationManager;

  @Autowired
  private DaijiaApi daijiaApi;

  /**
   * V4查询单个订单.
   */
  @RequestMapping(value = {"findOne"}, method = RequestMethod.GET)
  public ResultTransport findOne(
      @RequestParam("orderId") Long orderId,
      @RequestParam("appKey") String appKey) {
    WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);
    ResultTransport resultTransport =
        freightManager.findOrderV4(orderId, wxApplication.getAddress(), appKey);
    return resultTransport;
  }

  /**
   * V4查询货运类型.
   */
  @RequestMapping(value = {"queryType"}, method = RequestMethod.GET)
  public ResultTransport queryType(
      @RequestParam("companyId") Long companyId,
      @RequestParam("appKey") String appKey) {
    ResultTransport resultTransport = freightManager.queryTypeV4(companyId, appKey);
    return resultTransport;
  }

  /**
   * V4抢单.
   */
  @RequestMapping(value = {"grab"}, method = RequestMethod.POST)
  public ResultTransport grab(
      @RequestParam("orderId") Long orderId,
      @RequestParam("employToken") String employToken,
      @RequestParam("appKey") String appKey) {
    WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);

    ResultTransport employResult =
        daijiaApi.findDriverV4Token(wxApplication.getAddress(), employToken);
    if (employResult.getCode() != 0) {
      return employResult;
    }
    Map<String, Object> emap = (Map<String, Object>) employResult.getData();

    Long employId = Long.valueOf(emap.get("employId").toString());
    Long companyId = Long.valueOf(emap.get("companyId").toString());
    Double employScore = Double.valueOf(emap.get("employScore").toString());
    String employName = (String) emap.get("employName");
    String employPhone = (String) emap.get("employPhone");
    String employAccount = (String) emap.get("employAccount");
    String employPhoto = (String) emap.get("employPhoto");
    int employJobStatus = (int) emap.get("employJobStatus");
    int employStatus = (int) emap.get("employStatus");
    String employJobType = (String) emap.get("employJobType");

    ResultTransport transport = new ResultTransport();
    if (employJobType.contains("freight") && (employJobStatus == 3 || employJobStatus == -1)
        && employStatus == 1) { // 包含货运业务,工作状态为货运或无任务,服务状态为空闲
      transport = freightManager.grabV4(orderId, employId, employName, employPhone, companyId,
              employAccount, employPhoto, employScore, appKey);

      if (transport.getCode() == 0) {
        daijiaApi.pushOrderAcceptV4(wxApplication.getAddress(), orderId, employId);
      }
    } else {
      transport.setCode(-21);
      transport.setMessage("服务人员状态错误");
    }
    return transport;
  }

  /**
   * V4接单.
   */
  @RequestMapping(value = {"jiedan"}, method = RequestMethod.POST)
  public ResultTransport jiedan(
      @RequestParam("orderId") Long orderId,
      @RequestParam("employToken") String employToken,
      @RequestParam("appKey") String appKey) {

    WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);

    ResultTransport instanceResult = freightManager.jiedanV4(orderId, appKey);

    if (instanceResult.getCode() == 0) {
      Map<String, Object> map = (Map<String, Object>) instanceResult.getData();
      Object object = map.get("employId");
      Long employId = Long.valueOf(object.toString());
      daijiaApi.pushOrderAcceptV4(wxApplication.getAddress(), orderId, employId);
    }
    return instanceResult;
  }

  /**
   * V4拒单.
   */
  @RequestMapping(value = {"refuse"}, method = RequestMethod.POST)
  public ResultTransport refuse(
      @RequestParam("orderId") Long orderId,
      @RequestParam("employToken") String employToken, 
      @RequestParam("cause") String cause,
      @RequestParam("appKey") String appKey) {
    WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);

    ResultTransport employResult =
        daijiaApi.findDriverV4Token(wxApplication.getAddress(), employToken);
    if (employResult.getCode() != 0) {
      return employResult;
    }
    Map<String, Object> emap = (Map<String, Object>) employResult.getData();

    Long employId = Long.valueOf(emap.get("employId").toString());

    ResultTransport transport = freightManager.refuseV4(orderId, cause, appKey);
    if (transport.getCode() == 0) {
      transport =
          daijiaApi.pushRefuseFreightV4(wxApplication.getAddress(), orderId, employId, cause);
    }
    return transport;
  }

  /**
   * V4到达预约地开始等待.
   */
  @RequestMapping(value = {"wait"}, method = RequestMethod.POST)
  public ResultTransport wait(
      @RequestParam("orderId") Long orderId,
      @RequestParam("appKey") String appKey) {
    WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);

    ResultTransport transport = freightManager.waitV4(orderId, appKey);

    if (transport.getCode() == 0) {
      daijiaApi.pushWaitV4(wxApplication.getAddress(), orderId); // 到达预约地推送
    }

    return transport;
  }

  /**
   * V4执行.
   */
  @RequestMapping(value = {"run"}, method = RequestMethod.POST)
  public ResultTransport runOrder(
      @RequestParam("orderId") Long orderId,
      @RequestParam("appKey") String appKey) {
    ResultTransport transport = freightManager.runV4(orderId, appKey);
    return transport;
  }

  /**
   * V4到达某个预约点.
   */
  @RequestMapping(value = {"reach"}, method = RequestMethod.POST)
  public ResultTransport reach(
      @RequestParam("orderId") Long orderId,
      @RequestParam("waypointId") Long waypointId,
      @RequestParam("appKey") String appKey)
      throws Exception {
    ResultTransport resultTransport = freightManager.reachV4(orderId, waypointId, appKey);
    return resultTransport;
  }

  /**
   * V4销单.
   */
  @RequestMapping(value = {"cancel"}, method = RequestMethod.POST)
  public ResultTransport cancel(
      @RequestParam("orderId") Long orderId,
      @RequestParam("cause") String cause,
      @RequestParam("appKey") String appKey) {
    WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);

    ResultTransport transport = freightManager.cancelV4(orderId, cause, appKey);
    if (transport.getCode() == 0) {
      transport = daijiaApi.pushOrderCancelV4(
          wxApplication.getAddress(), orderId, true, cause); // 销单推送
    }
    return transport;
  }

  /**
   * 获取订单结算数据.
   */
  @RequestMapping(value = {"completeTaskData"}, method = RequestMethod.GET)
  public ResultTransport completeTaskData(
      @RequestParam("orderId") Long orderId,
      @RequestParam("appKey") String appKey) {
    WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);

    ResultTransport resultTransport =
        daijiaApi.completeTaskFreightV4(wxApplication.getAddress(), orderId);

    return resultTransport;
  }

  /**
   * 确认费用.
   */
  @RequestMapping(value = {"confirmTask"}, method = RequestMethod.POST)
  public ResultTransport confirmTask(
      @RequestParam("orderId") Long orderId,
      @RequestParam("realCash") Double realCash, 
      @RequestParam("guoluMoney") Double guoluMoney,
      @RequestParam("yuanchengMoney") Double yuanchengMoney,
      @RequestParam("otherMoney") Double otherMoney,
      @RequestParam("mileage") Double mileage,
      @RequestParam("waitedTime") Integer waitedTime,
      @RequestParam("travelTime") Integer travelTime,
      @RequestParam("startPrice") Double startPrice,
      @RequestParam("mileagePrice") Double mileagePrice,
      @RequestParam("runTimePrice") Double runTimePrice,
      @RequestParam(required = false, value = "waitPrice") Double waitPrice,
      @RequestParam(required = false, value = "memo") String memo,
      @RequestParam(required = false, value = "jsonLocs") String jsonLocs,
      @RequestParam("appKey") String appKey) {
    WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);

    ResultTransport transport =
        daijiaApi.confirmTaskFreightV4(wxApplication.getAddress(), orderId, realCash, guoluMoney,
            yuanchengMoney, otherMoney, mileage, waitedTime, travelTime, startPrice, mileagePrice,
            runTimePrice, waitPrice, memo, jsonLocs);

    return transport;
  }

  /**
   * 实时检测是否被结算.
   */
  @RequestMapping(value = {"checkTask"}, method = RequestMethod.GET)
  public ResultTransport checkTask(
      @RequestParam("orderId") Long orderId,
      @RequestParam("appKey") String appKey) {
    WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);

    ResultTransport resultTransport =
        daijiaApi.checkTaskFreightV4(wxApplication.getAddress(), orderId);

    return resultTransport;
  }

  /**
   * 最后结算接口.
   * @param payType 支付类型: helppay(代付) sign(签单) balance(余额)
   */
  @RequestMapping(value = {"finishTask"}, method = RequestMethod.POST)
  public ResultTransport finishTask(
      @RequestParam("orderId") Long orderId,
      @RequestParam("payType") String payType,
      @RequestParam(required = false, value = "who") String who,
      @RequestParam("appKey") String appKey) {
    WxApplication wxApplication = wxApplicationManager.findByAppKey(appKey);

    ResultTransport transport =
        daijiaApi.finishTaskFreightV4(wxApplication.getAddress(), orderId, payType, who);

    return transport;
  }

}
